<?php

/**
 * Выставка
 *
 * @package    sf_sandbox
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfPropelFormTemplate.php 10377 2008-07-21 07:10:32Z dwhittle $
 */
class Type14Form extends BaseMaterialForm
{
  public function configure()
  {
  	
  		unset( 

  		$this['updated_at'],  
  		$this['user_id'], 
  		$this['click'], 
  		$this['country'],
  		$this['custom_int3'],
  		$this['custom_text2'],
  		$this['custom_text3'],
  		$this['custom_char2'],
  		$this['created_at'],
  		$this['stat'],
  		$this['parent_id'],
  		$this['link'])
  		;
  		
   		$c = new Criteria ( );	
		$c->addAnd ( FolderPeer::TREE_PARENT, 1, Criteria::NOT_EQUAL );
		$c->addAnd ( MaterialPeer::ID, 'tt' ); 		
  		
  		$this->setWidgets(array(
      'id'               => new sfWidgetFormInputHidden(array(),array('size'=>'60')),
      'folders_array'               => new sfWidgetFormInputHidden(array(),array('size'=>'60')),
      'type_id'           => new sfWidgetFormInputHidden(array(), array('value' =>'14', 'class'=>'test')),
  	  'title'               => new sfWidgetFormInput(array(),array('class'   =>  'material_title','style'=>'width:620px;')),	
  	  'short_content' => new sfWidgetFormCKEditor (array('jsoptions'=>array('filebrowserBrowseUrl'=>sfConfig::get('app_ck_filebrouser'),
  	                                                                             'filebrowserUploadUrl' => sfConfig::get('app_ck_fileupload'),
                                                                                 'toolbar ' => sfConfig::get('app_ck_toolbar'))) ),	
  	   'content' => new sfWidgetFormCKEditor (array('jsoptions'=>array('filebrowserBrowseUrl'=>sfConfig::get('app_ck_filebrouser'),
  	                                                                             'filebrowserUploadUrl' => sfConfig::get('app_ck_fileupload'),
                                                                                 'toolbar ' => sfConfig::get('app_ck_toolbar'))) ),
  	  'photo_path' =>  new inputImageTag (  array (), array('mat_id'=>$this->object->getId(),'folder'=>'journal') ),
  	  'pub'      => new sfWidgetFormInputCheckbox(),
      'custom_date1'      => new sfWidgetFormDate(array(),array('format'=>'%day%%month%%year%')),
      'custom_date2'      => new sfWidgetFormDate(array(),array('format'=>'%day%%month%%year%')),
  	  'custom_date3'      => new sfWidgetFormDate(array(),array('format'=>'%day%%month%%year%')),
  	  'custom_timestamp1'      => new sfWidgetFormDate(array(),array('format'=>'%day%%month%%year%')),
      'city' => new sfWidgetFormInput ( array (), array ('size' => '80' ) ), 	 
  	  'adres' => new sfWidgetFormInput ( array (), array ('size' => '80' ) ), 
  	  'custom_text1' => new sfWidgetFormCKEditor (array('jsoptions'=>array('filebrowserBrowseUrl'=>sfConfig::get('app_ck_filebrouser'),
  	                                                                             'filebrowserUploadUrl' => sfConfig::get('app_ck_fileupload'),
                                                                                 'toolbar ' => sfConfig::get('app_ck_toolbar'))) ),
 	  'custom_file1' =>  new inputImage1Tag (  array (), array('mat_id'=>$this->object->getId(),'folder'=>'journal') ),

  		  	    'mat2_folder_list' => new sfWidgetFormPropelChoiceMany ( array ('model' => 'Folder', 
  	                                                                    'multiple' => true,
  	                                                                    'expanded' => true,
  		                                                                'renderer_options' => array('formatter' =>array($this,'myFormatter')),
  	                                                                    'criteria' => $c ) ),
  		
  	 
  	  'seo_description' => new sfWidgetFormTextarea(array(),array('style'=>'width:200px')),
  	  'seo_keyword' => new sfWidgetFormTextarea(array(),array('style'=>'width:200px'))
    ));

   
      
        
/*
     *'content' => new sfWidgetFormTextareaTinyMCE(array(),array('class'=>'material_content','width'=>'600px')),
     *  
     */
    
         /////Ссылка на форум
		 if(sfConfig::get('app_use_link_forum')==true)
		 {
		 	$this->widgetSchema['custom_char3'] =  new sfWidgetFormInput(array(),array('size'=>'60'));
		 }
		 ////
    
  		
  		$this->widgetSchema->setLabels(array(  
  		'short_content'   => 'Краткое описание:',
  		'content'   => 'Тематические разделы:',
  		'title'   => 'Название мероприятия:',
  		'photo_path'   => 'Логотип мероприятия:', 
  		'mat2_folder_list'   => 'Регион:',
  		'region_id'   => 'Регион:',
  		'custom_date1'   => 'Дата показа "C":',
  		'custom_date2'   => 'Дата показа "ПО":',
  		'custom_date3'   => 'Дата начала "С":',
  		'custom_timestamp1'   => 'Дата конца "ПО":',
  		'pub'   => 'Опубликован:',
  		'city'   => 'Город:',
  		'adres'   => 'Адрес:',
  		'custom_text1' =>'Организатор:',
  		 'custom_char3'   => 'Ссылка на форум: ',
  		'custom_file1'   => 'Логотип организатора: ',
  		'link'   => 'Ссылка:',
  	 ));
  		

     $this->validatorSchema['photo_path'] = new sfValidatorFile(array(
        'required'   => false,
        'path'       => sfConfig::get('sf_web_dir').'/uploads/material/journal',
        'mime_types' => 'web_images',
        'max_size'   => 380000, // 360 kB
        'validated_file_class' => 'sfMatJournalValidatedFile'
        ));
        
     $this->validatorSchema['custom_file1'] = new sfValidatorFile(array(
        'required'   => false,
        'path'       => sfConfig::get('sf_web_dir').'/uploads/material/journal',
        'mime_types' => 'web_images',
        'max_size'   => 380000, // 360 kB
        'validated_file_class' => 'sfMatJournalValidatedFile'
        ));        
        
  	$this->validatorSchema['custom_date1'] =  new sfValidatorDate(array('required' => false));
  	$this->validatorSchema['custom_date2'] =  new sfValidatorDate(array('required' => false));
    $this->validatorSchema['custom_int2'] = new sfValidatorNumber(array('required' => true),array('required'=>'НЕ УСТАНОВЛЕН РЕГИОН'));    
           
       
        $this->validatorSchema->setOption('allow_extra_fields', true);    
  		 
  		 $this->widgetSchema->setNameFormat('material[%s]');	 
  		 
  		//    $js_path = '/js/tinymce/tiny_mce.js';
        //    sfContext::getInstance()->getResponse()->addJavascript($js_path);
            
        
            
            
  
  }
  
  
  
  	public function saveMat2FolderList($con = null) {
		if (! $this->isValid ()) {
			throw $this->getErrorSchema ();
		}
		
		if (! isset ( $this->widgetSchema ['mat2_folder_list'] )) {
			// somebody has unset this widget
			return;
		}
		
		if (is_null ( $con )) {
			$con = $this->getConnection ();
		}
		
		$c = new Criteria ( );
		$c->add ( Mat2FolderPeer::MATERIAL_ID, $this->object->getPrimaryKey () );
		Mat2FolderPeer::doDelete ( $c, $con );
		
		$values = $this->getValue ( 'mat2_folder_list' );
		if (is_array ( $values )) {
			
			foreach ( $values as $value ) {
				
				if(!is_numeric($value)) break;
				
				$obj = new Mat2Folder ( );
				$obj->setMaterialId ( $this->object->getPrimaryKey () );
				$obj->setFolderId ( $value );
				$obj->save ();
				
				$f = FolderPeer::retrieveByPK ( $value );
				
				////Добавляем счетчик
				FolderPeer::recount ( $f );
				
				if ($f->hasParent ()) {
					$par = $f->retrieveParent ();
					
					if (! in_array ( $par->getId (), $values )) {
						
						$this->saverec ( $par, $this->object->getPrimaryKey () );
						////Добавляем счетчик
						if ($par->hasParent ()) {
							$parr = $par->retrieveParent ();
							
							if (! in_array ( $parr->getId (), $values )) {
								//print_r($values).'-'.$parr->getId();
						         ////Добавляем счетчик
				                
								$this->saverec ( $parr, $this->object->getPrimaryKey () );
								if ($parr->hasParent ()) {
									$parrr = $parr->retrieveParent ();
									if (! in_array ( $parrr->getId (), $values )) {
										$this->saverec ( $parrr, $this->object->getPrimaryKey () );
						                ////Добавляем счетчик
				                       								
									}
								}
							
							}
						
						}
					}
				
				}
			
			}
			
			
		}
		
	}
	
	public function saverec($par, $mat_id) {
		
		//echo $par->getId().'-'.$mat_id;
		$c = new Criteria ( );
		$c->add ( Mat2FolderPeer::FOLDER_ID, $par->getId () );
		$c->addAnd ( Mat2FolderPeer::MATERIAL_ID, $mat_id );
		$cnt = Mat2FolderPeer::doCount ( $c );
		if ($cnt == 0) {
			$re = new Mat2Folder ( );
			$re->setMaterialId ( $mat_id );
			$re->setFolderId ( $par->getId () );
			$re->save ();
		}
	
	}
 
  
 function myFormatter($widget, $inputs)
  {
  	$str= '';
    $str = get_partial('region_checked_folders',array('arr'=>$this['mat2_folder_list']->getValue(),'scope_id'=>sfConfig::get('app_scope_region'),'mat_id'=>$this->getObject()->getId()));
    return $str;
  }	
   
}
